﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using CSNN.GUI;

namespace CSNN
{
    /// <summary>
    /// Логика взаимодействия для WindowAddTrainingImage.xaml
    /// </summary>
    public partial class WindowAddTrainingImage : Window
    {
        #region Члены класса

        /// <summary>
        /// Флаг представления изображения
        /// </summary>
        public bool IsImage;

        /// <summary>
        /// Количество входов/выходв нейронной сети
        /// </summary>
        private int _nInputs, _nOutputs;

        /// <summary>
        /// Список обучающих множеств
        /// </summary>
        private BindingList<TrainingImageInList> _trainingImageList;

        /// <summary>
        /// Список входов
        /// </summary>
        private BindingList<NewImage> _inputs;

        /// <summary>
        /// Список выходов
        /// </summary>
        private BindingList<NewImage> _outputs;

        #endregion

        public WindowAddTrainingImage()
        {
            InitializeComponent();

            this.IsImage = false;

            _inputs = new BindingList<NewImage>();
            _outputs = new BindingList<NewImage>();
        }

        /// <summary>
        /// Усиновка значений входов/выходов нейронной сети
        /// </summary>
        /// <param name="nInputs">Количество входных сигналов</param>
        /// <param name="nOutputs">Количество выходных сигналов</param>
        /// <param name="trainingImageList">Список обучающих множеств</param>
        public void SetInOutPuts(int nInputs, int nOutputs, BindingList<TrainingImageInList> trainingImageList)
        {
            if (nInputs <= 0 || nOutputs <= 0 || trainingImageList == null)
            {
                throw new ArgumentException("Ошибка! Переданы не корректные аргументы!");
            }
                        
            this._nInputs = nInputs;
            this._nOutputs = nOutputs;
            this._trainingImageList = trainingImageList;
                        
            for (int i = 0; i < _nInputs; i++)
            {
                _inputs.Add(new NewImage(0, "Input "+i.ToString()));
            }
            for (int i = 0; i < _nOutputs; i++)
            {
                _outputs.Add(new NewImage(0, "Output " + i.ToString()));
            }
            
            this.dataGridInputs.ItemsSource = _inputs;
            this.dataGridInputs.IsReadOnly = false;

            this.DataGridOutputs.ItemsSource = _outputs;
            this.DataGridOutputs.IsReadOnly = false;

            scrollViewerImage.IsEnabled = false;
            GridText.IsEnabled = true;
            
            GridText.Visibility = Visibility.Visible;

            buttonSubmit.Visibility = scrollViewerImage.Visibility
                                    = labelWeight.Visibility
                                    = textBoxWeight.Visibility
                                    = labelHigth.Visibility
                                    = textBoxHeigth.Visibility
                                    = image1.Visibility
                                    = Visibility.Hidden;
        }

        /// <summary>
        /// Обработчик переключателя режимов Изображение/Текст
        /// </summary>
        private void buttonToPicture_Click(object sender, RoutedEventArgs e)
        {
            if (!IsImage)
            {
                this.buttonToPicture.Content = "ON";
                buttonToPicture.Foreground = Brushes.Black;
                buttonToPicture.Background = Brushes.White;
                                
                IsImage = true;

                GridText.Visibility = Visibility.Hidden;

                buttonSubmit.Visibility = scrollViewerImage.Visibility 
                                        = labelWeight.Visibility
                                        = textBoxWeight.Visibility
                                        = labelHigth.Visibility
                                        = textBoxHeigth.Visibility
                                        = image1.Visibility
                                        = Visibility.Visible;
            }
            else
            {
                this.buttonToPicture.Content = "OFF";
                
                buttonToPicture.Foreground = Brushes.White;
                buttonToPicture.Background = Brushes.Black;
                
                IsImage = false;

                GridText.Visibility = Visibility.Visible;

                buttonSubmit.Visibility = scrollViewerImage.Visibility
                                        = labelWeight.Visibility 
                                        = textBoxWeight.Visibility 
                                        = labelHigth.Visibility 
                                        = textBoxHeigth.Visibility 
                                        = image1.Visibility
                                        = Visibility.Hidden;
            }
        }

        /// <summary>
        /// Обработчик клика кнопки "Добавить образ"
        /// </summary>
        private void buttonAddImage_Click(object sender, RoutedEventArgs e)
        {
            double[] inPuts = new double[_nInputs];
            double[] outPuts = new double[_nOutputs];

            for (int i = 0; i < _nInputs; i++)
            {
                inPuts[i] = _inputs[i].Unit;
            }
            for (int i = 0; i < _nOutputs; i++)
            {
                outPuts[i] = _outputs[i].Unit;
            }

            TrainingImageInList image = new TrainingImageInList(_nInputs, _nOutputs, _trainingImageList.Count, textBoxDiscription.Text);
            image.SetTrainingImage(inPuts, outPuts);

            _trainingImageList.Add(image);
        }
    }
}
